﻿using DevExpress.Web.ASPxEditors;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.admin
{
    public partial class dangky : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2010; i >= 1950; i--)
                {
                    cbNam.Items.Add(i.ToString(), i);
                }

                cbNgay.SelectedIndex = 0;
                cbThang.SelectedIndex = 0;
                cbNam.SelectedIndex = 30;
                cbTrangthai.SelectedIndex = 0;
                Bindata_Location_Office();
            }
        }

        int timNguoi(String uName, int tuvan)
        {
            int id = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.UserName == uName
                           select k;

                foreach (UserDB u in list)
                {
                    id = u.ID;
                    if (tuvan == 1)
                        hdftuvan.Value = u.Lastname + " " + u.FirtName;
                    else if (tuvan == 2)
                        hdfgioithieu.Value = u.Lastname + " " + u.FirtName;
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return id;
        }

        int timEmail(String email)
        {
            int id = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.email == email
                           select k;

                foreach (UserDB u in list)
                {
                    id = u.ID;
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return id;
        }

        int timPhone(String phone)
        {
            int id = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.phone == phone
                           select k;

                foreach (UserDB u in list)
                {
                    id = u.ID;
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return id;
        }

        void Bindata_Office()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            if (cbxChinhanh.Value.ToString() == "0")
            {
                cbVanphong.Text = "";
                cbVanphong.Items.Clear();
                cbVanphong.Enabled = false;
            }
            else
                try
                {
                    cbVanphong.Enabled = true;
                    dx.Connection.Open();
                    var list = from k in dx.Offices
                               where k.Location_OfficeID == int.Parse(cbxChinhanh.Value.ToString()) && k.Acti == true
                               select new { k.ID, k.OfficeCode, k.OfficeName };
                    cbVanphong.DataSource = list;
                    cbVanphong.TextField = "OfficeName";
                    cbVanphong.ValueField = "ID";
                    cbVanphong.DataBind();

                    if (cbVanphong.Items.Count > 0)
                    {
                        ListEditItem item = new ListEditItem();
                        item.Index = 0;
                        item.Text = "------ Chọn văn phòng ----";
                        item.Value = 0;
                        item.Selected = true;
                        cbVanphong.Items.Insert(0, item);
                    }
                    else
                        cbVanphong.Enabled = true;
                }
                catch (Exception) { }
                finally { dx.Connection.Close(); }
        }

        void Bindata_Location_Office()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.Location_Offices
                           where k.Acti == true
                           select new { k.ID, k.LocationCode, k.LocationName };
                cbxChinhanh.DataSource = list;
                cbxChinhanh.TextField = "LocationName";
                cbxChinhanh.ValueField = "ID";
                cbxChinhanh.DataBind();
                ListEditItem item = new ListEditItem();
                item.Index = 0;
                item.Text = "------ Chọn chi nhánh ----";
                item.Value = 0;
                item.Selected = true;
                cbxChinhanh.Items.Insert(0, item);
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        protected void cbxChinhanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bindata_Office();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lbthongbao.Text = "";
            int tontai = timNguoi(txtUserName.Text.Trim(), 0);
            int idTuvan = 0;
            int email = 0;
            if (tontai == 0)
            {
                if (txtUserName.Text != "" && txtNguoituvan.Text != "" && txtNguoigioithieu.Text != "" && txtLassname.Text != ""
                    && txtFirstname.Text != "" && txtDienthoai.Text != "" && txtEmail.Text != "" && txtDiachi.Text != "" && txtCMND.Text != "")
                {
                    tontai = timNguoi(txtNguoigioithieu.Text.Trim(), 2);
                    idTuvan = timNguoi(txtNguoituvan.Text.Trim(), 1);
                    email = timEmail(txtEmail.Text.Trim());
                    float h = 0;
                    try
                    {
                        h = float.Parse(txtMoney.Text);
                    }
                    catch (Exception) { }

                    if (tontai > 0 && idTuvan > 0 && email == 0 && h >= 100)
                    {

                        DataClasses1DataContext dx = new DataClasses1DataContext();
                        try
                        {
                            dx.Connection.Open();
                            float m = float.Parse(txtMoney.Value.ToString());
                            UserDB u = new UserDB();
                            u.address = txtDiachi.Text;
                            u.cmnd = txtCMND.Text;
                            u.email = txtEmail.Text;
                            u.FirtName = txtFirstname.Text;
                            u.Lastname = txtLassname.Text;
                            u.Fullname = txtLassname.Text + " " + txtFirstname.Text;
                            u.gioitinh = cbGioitinh.Text;
                            u.namsinh = int.Parse(cbNam.Value.ToString());
                            u.thangsinh = int.Parse(cbThang.Value.ToString());
                            u.ngaysinh = int.Parse(cbNgay.Value.ToString());
                            u.UserName = txtUserName.Text;

                            string trangthai = cbTrangthai.Value.ToString();
                            if (m == 590)
                                trangthai = "0";
                            else if (m < 590 && m > 99)
                                trangthai = "1";
                            else
                                trangthai = "2";
                            switch (trangthai)
                            {
                                case "0":
                                    if (cbGioitinh.Text == "Nam")
                                        u.imgvata = "male_blue16.png";
                                    else
                                        u.imgvata = "female_blue16.png";
                                    break;
                                case "1":
                                    if (cbGioitinh.Text == "Nam")
                                        u.imgvata = "male_yealow16.png";
                                    else
                                        u.imgvata = "female_yelow16.png";
                                    break;
                                case "2":
                                    if (cbGioitinh.Text == "Nam")
                                        u.imgvata = "male_red16.png";
                                    else
                                        u.imgvata = "female_red16.png";
                                    break;
                            }

                            u.phone = txtDienthoai.Text.Trim();
                            u.kichhoat = false;
                            u.flag = 1;
                            u.Password = "4a7d8a3ed6e79ce1";
                            u.ngaydangky = DateTime.Now;
                            u.ngaythamgia = DateTime.Now;
                            u.money = float.Parse(txtMoney.Value.ToString());
                            u.parentID = tontai;
                            u.UserIDTuvan = idTuvan;
                            u.TuvanName = hdftuvan.Value;
                            u.nguoigioithieu = hdfgioithieu.Value;
                            u.capbac = "Cộng tác viên";
                            u.Location_OfficeID = int.Parse(cbxChinhanh.Value.ToString());
                            if (cbVanphong.Text != "")
                                u.Office_ID = int.Parse(cbVanphong.Value.ToString());
                            else
                                u.Office_ID = 0;
                            u.visible = true;
                            dx.UserDBs.InsertOnSubmit(u);
                            dx.SubmitChanges();
                            lbthongbao.ForeColor = Color.Blue;
                            lbthongbao.Text = "Chúc mừng đăng ký thành công! admin xẽ duyệt và acti cho tài khoản của bạn ";
                            txtCMND.Text = "";
                            txtDiachi.Text = "";
                            txtDienthoai.Text = "";
                            txtEmail.Text = "";
                            txtFirstname.Text = "";
                            txtLassname.Text = "";
                            txtMoney.Text = "";
                            txtNguoigioithieu.Text = "";
                            txtNguoituvan.Text = "";
                            txtUserName.Text = "";
                            txtVisa.Text = "";

                        }
                        catch (Exception)
                        {
                            lbthongbao.ForeColor = Color.Red;
                            lbthongbao.Text = "Đã xảy ra lỗi trong quá trình xử lý! xin vui lòng thử lại sau";
                        }
                        finally { dx.Connection.Close(); }
                    }
                    else if (tontai == 0)
                    {
                        lbthongbao.ForeColor = Color.Red;
                        lbthongbao.Text = "Ngườ giới thiệu " + txtNguoigioithieu.Text.Trim() + " không có trong hệ thống! ";
                    }
                    else if (idTuvan == 0)
                    {
                        lbthongbao.ForeColor = Color.Red;
                        lbthongbao.Text = "Ngườ tư vấn " + txtNguoituvan.Text.Trim() + " không có trong hệ thống! ";
                    }
                    else if (email > 0)
                    {
                        lbthongbao.ForeColor = Color.Red;
                        lbthongbao.Text = "Email: " + txtEmail.Text.Trim() + " đã tồn tại! ";
                    }
                }
            }
            else
            {
                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "UserName này đã tồn tại rồi! Vui lòng chọn UserName khác";
            }
        }
    }
}